% Load data from temperature files
% hyzhou, 11/21/2017
% Relevant data should be pre-loaded
% Inputs:
%   file_type: type of file for the acquisition, whether it is calibration,
%       temperature, or something else
%   info_names: cell containing the names
% Outputs:
%   info_list: list of information

function info_list = load_temp_data(file_type,info_names,varargin)

switch file_type
    case 'ESR'
        gResultSave = varargin{1};
        bPlot = varargin{2};
        info_list = zeros([size(gResultSave,1)*size(gResultSave,2),length(info_names)]);
        for i = 1:size(gResultSave,1)*size(gResultSave,2)
            gSeqParameter = gResultSave{i}.gSeqParameter;
            gTrackCount = gResultSave{i}.gTrackCount;
            gTrackData = gResultSave{i}.gTrackData;
            gScan = gResultSave{i}.gScan;
            gResultData = gResultSave{i}.gResultData;
            try
                errs = diff(confint(gResultData.fit_res),1)/4;
            catch
                1
            end
            for j = 1:length(info_names)
                name = info_names{j};
                
                switch name
                    case {'sens_init','sens_fin','Power','InfraredmA','MeasTime','slope',...
                            'fit_error','slope_err','GreenmA'}
                        eval(['info_list(j)=gSeqParameter.',...
                            name,';'])
                    case 'fl'   % fluorescence extracted from average of actual measurements
                        info_list(i,j)=mean(gTrackCount(:,2));
                    case 'NVx'
                        info_list(i,j)=mean(gTrackData(:,2))/gScan.vol_to_dist;
                    case 'NVy'
                        info_list(i,j)=mean(gTrackData(:,3))/gScan.vol_to_dist;
                    case 'NVz'
                        info_list(i,j)=mean(gTrackData(:,4));
                    case 'fcen'
                        info_list(i,j)=(gSeqParameter.Freq1+...
                            gSeqParameter.Freq2+gSeqParameter.delta_w)/2;
                    case 'linewidth'
                        info_list(i,j)=gSeqParameter.Freq2-...
                            gSeqParameter.Freq1;
                    case 'fit_cen'
                        info_list(i,j)=gResultData.fit_res.b;
                    case 'fit_cen_err'
                        info_list(i,j)=errs(2);
                    case 'fit_width'
                        info_list(i,j)=gResultData.fit_res.c;
                    case 'fit_width_err'
                        info_list(i,j)=errs(3);
                    case 'fit_dist'
                        info_list(i,j)=gResultData.fit_res.d;
                    case 'fit_dist_err'
                        info_list(i,j)=errs(4);
                    case {'SingleLor','DoubleLor','Heaviside'}
                        x = gResultData.Freq'/1e9;
                        y = mean(gResultData.ESR,1)';
                        opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
                        switch name
                            case 'SingleLor'
                                ft = fittype( '1-a./(((x-b)./c).^2 + 1)', 'independent', 'x', 'dependent', 'y' );
                                opts.StartPoint = [0.05 2.87 0.01];
                                
                            case 'DoubleLor'
                                ft = fittype( '1-a./(((x-b-e)./c).^2 + 1)-d./(((x-b+e)./f).^2 + 1)', 'independent', 'x', 'dependent', 'y' );
                                opts.StartPoint = [0.05 2.87 0.01 0.05 0.004 0.01];
                            case 'Heaviside'
                                ft = fittype('1 - heaviside(b-x).*a./(1+((x-b+d)/c).^2) - heaviside(x-b).*a./(1+((x-b-d)/c).^2)');
                                opts.StartPoint = [0.1 2.868 0.01 0.001];
                        end
                        [fitresult, ~] = fit( x, y, ft, opts );
                        info_list(i,j) = fitresult.b;
                    otherwise
                        error('Data type not supported!')
                end
            end
        end
    case 'calibration'
        gSeqParameter = varargin{1}{1};
        gScan = varargin{1}{2};
        gTrackCount = varargin{1}{3};
        gTrackData = varargin{1}{4};
        info_list = zeros([1,length(info_names)]);
        for j = 1:length(info_names)
            name = info_names{j};
            switch name
                case {'sens_init','sens_fin','Power','InfraredmA','MeasTime','slope',...
                        'fit_error','slope_err','GreenmA'}
                    eval(['info_list(j)=gSeqParameter.',...
                        name,';'])
                case 'fl'   % fluorescence extracted from average of actual measurements
                    info_list(j)=mean(gTrackCount(:,2));
                case 'NVx'
                    info_list(j)=mean(gTrackData(:,2))/gScan.vol_to_dist;
                case 'NVy'
                    info_list(j)=mean(gTrackData(:,3))/gScan.vol_to_dist;
                case 'NVz'
                    info_list(j)=mean(gTrackData(:,4));
                case 'fcen'
                    info_list(j)=(gSeqParameter.Freq1+...
                        gSeqParameter.Freq2+gSeqParameter.delta_w)/2;
                case 'linewidth'
                    info_list(j)=gSeqParameter.Freq2-...
                        gSeqParameter.Freq1;
                otherwise
                    error('Data type not supported!')
            end
        end
    case 'temperature'
        gSeqParameter = varargin{1}{1};
        gScan = varargin{1}{2};
        gTrackCount = varargin{1}{3};
        gTrackData = varargin{1}{4};
        gResultData = varargin{1}{5};
        info_list = zeros([1,length(info_names)]);
        for j = 1:length(info_names)
            name = info_names{j};
            switch name
                case {'sens_init','sens_fin','Power','InfraredmA','MeasTime','slope',...
                        'fit_error','slope_err','GreenmA'}
                    eval(['info_list(j)=gSeqParameter.',...
                        name,';'])
                case {'ListTemp_on','ListTemp_off'}
                    eval(['info_list(j)=mean(gResultData.',name,');'])
                case {'ListTemp_on_err','ListTemp_off_err'}
                    name = name(1:end-4);
                    eval(['info_list(j)=std(gResultData.',name,...
                        ')/sqrt(length(gResultData.',name,'));'])
                case 'ListTemp_diff'
                    info_list(j)=mean(gResultData.ListTemp_on-gResultData.ListTemp_off);
                case 'ListTemp_diff_err'
                    info_list(j)=std(gResultData.ListTemp_on-gResultData.ListTemp_off)/sqrt(length(gResultData.ListTemp_on));
                case 'fl'   % fluorescence extracted from average of actual measurements
                    info_list(j)=mean(gTrackCount(:,2));
                case 'NVx'
                    info_list(j)=mean(gTrackData(:,2))/gScan.vol_to_dist;
                case 'NVy'
                    info_list(j)=mean(gTrackData(:,3))/gScan.vol_to_dist;
                case 'NVz'
                    info_list(j)=mean(gTrackData(:,4));
                case 'fcen'
                    info_list(j)=(gSeqParameter.Freq1+...
                        gSeqParameter.Freq2+gSeqParameter.delta_w)/2;
                case 'linewidth'
                    info_list(j)=gSeqParameter.Freq2-gSeqParameter.Freq1;
                case 'ShotNoise_exp'
                    [~,info_list(j),~]=get_shot_noise_limit(gResultData.FullListfl,...
                        gSeqParameter,gResultData.MeasTime);
                case 'ShotNoise_th'
                    [info_list(j),~,~]=get_shot_noise_limit(gResultData.FullListfl,...
                        gSeqParameter,gResultData.MeasTime);
                otherwise
                    error('Data type not supported!')
            end
        end
    case 'gradient'
        gResultSave = varargin{1}{1};
        numNV = length(gResultSave);
        info_list = zeros([numNV,length(info_names)]);
        for i = 1:numNV
            for j = 1:length(info_names)
                name = info_names{j};
                switch name
                    case {'sens_fin','Power','InfraredmA','MeasTime','slope',...
                            'fit_error','slope_err','GreenmA'}
                        eval(['info_list(i,j)=gResultSave{i}.gSeqParameter.',...
                            name,';'])
                    case {'SumTemp_on','SumTemp_off','SumTemp_diff',...
                            'SumTemp_on_err','SumTemp_off_err','SumTemp_diff_err'}
                        eval(['info_list(i,j)=gResultSave{i}.gResultData.',...
                            name,';'])
                    case 'fl'   % fluorescence extracted from average of actual measurements
                        info_list(i,j)=mean(gResultSave{i}.gTrackCount(:,2));
                    case 'NVx'
                        info_list(i,j)=mean(gResultSave{i}.gTrackData(:,2))/gResultSave{i}.gScan.vol_to_dist;
                    case 'NVy'
                        info_list(i,j)=mean(gResultSave{i}.gTrackData(:,3))/gResultSave{i}.gScan.vol_to_dist;
                    case 'NVz'
                        info_list(i,j)=mean(gResultSave{i}.gTrackData(:,4));
                    case 'fcen'
                        info_list(i,j)=(gResultSave{i}.gSeqParameter.Freq1+...
                            gResultSave{i}.gSeqParameter.Freq2+gResultSave{i}.gSeqParameter.delta_w)/2;
                    case 'linewidth'
                        info_list(i,j)=gResultSave{i}.gSeqParameter.Freq2-...
                            gResultSave{i}.gSeqParameter.Freq1;
                    case 'ShotNoise_exp'
                        [~,info_list(i,j),~]=get_shot_noise_limit(gResultSave{i}.gResultData.FullListfl,...
                            gResultSave{i}.gSeqParameter,gResultSave{i}.gResultData.MeasTime);
                    case 'ShotNoise_th'
                        [info_list(i,j),~,~]=get_shot_noise_limit(gResultSave{i}.gResultData.FullListfl,...
                            gResultSave{i}.gSeqParameter,gResultSave{i}.gResultData.MeasTime);
                    otherwise
                        error('Data type not supported!')
                end
            end
        end
    case 'ESRgradient'
        gResultSave = varargin{1}{1};
        numNV = size(gResultSave,1);
        numGreen = size(gResultSave,2);
        info_list = zeros([numNV,length(info_names)]);
        for i = 1:numNV
            for k = 1:numGreen
                gResultTemp = gResultSave{i,k};
                errs = diff(confint(gResultTemp.gResultData.fit_res),1)/4;
                for j = 1:length(info_names)
                    name = info_names{j};
                    switch name
                        case 'GreenPow'
                            if isfield(gResultTemp.gSeqParameter,'green_pow_list')
                                info_list(i,j) = gResultTemp.gSeqParameter.green_pow_list(k);
                            else
                                info_list(i,j) = 0.25;
                            end
                        case {'photoncount','asymmetry'}
                            eval(['info_list(i,j)=gResultTemp.',...
                                name,';'])
                        case 'NVx'
                            info_list(i,j) = gResultTemp.NVloc(1);
                        case 'NVy'
                            info_list(i,j) = gResultTemp.NVloc(2);
                        case {'a1','a2','b','c','d','e'}
                            eval(['info_list(i,j) = abs(gResultTemp.gResultData.fit_res.',name,');'])
                        case {'a1_err','a2_err','b_err','c_err','d_err','e_err'}
                            ind = strcmp(name,{'a1_err','a2_err','b_err','c_err','d_err','e_err'});
                            info_list(i,j) = errs(ind);
                        otherwise
                            error('Data type not supported!')
                    end
                end
            end
        end
    case 'SweepGreen'
        gResultSave = varargin{1};
        delta_w = gResultSave{1,1,1}.gSeqParameter.delta_w;
        t_slope = gResultSave{1,1,1}.gSeqParameter.t_slope;
        info_list = zeros([size(gResultSave,1)*size(gResultSave,2),length(info_names),size(gResultSave,3)]);
        count = 0;
        for i = 1:size(gResultSave,1)   % loop over NVs
            NVloc = [mean(gResultSave{i,1,1}.gTrackData(:,2:3),1)/gResultSave{i,1,1}.gScan.vol_to_dist,...
                mean(gResultSave{i,1,1}.gTrackData(:,2:3),1)];
            for k = 1:size(gResultSave,2)   % loop over MW/green parameters
                count = count + 1;
                for rep = 1:size(gResultSave,3)
                    gSeqParameter = gResultSave{i,k,rep}.gSeqParameter;
                    gResultData = gResultSave{i,k,rep}.gResultData;
                    gTrackCount = gResultSave{i,k,rep}.gTrackCount;
                    for j = 1:length(info_names)
                        name = info_names{j};
                        switch name
                            case {'sens_init','sens_fin','Power','InfraredmA','MeasTime','slope',...
                                    'fit_error','slope_err','GreenmA'}
                                eval(['info_list(count,j,rep)=gSeqParameter.',...
                                    name,';'])
                            case {'ListTemp_on','ListTemp_off'}
                                eval(['info_list(count,j,rep)=mean(gResultData.',name,');'])
                            case {'ListTemp_on_err','ListTemp_off_err'}
                                name = name(1:end-4);
                                eval(['info_list(count,j,rep)=std(gResultData.',name,...
                                    ')/sqrt(length(gResultData.',name,'));'])
                                % ad hoc correction of number of repetitions, as
                                % we later just directly do averaging
                                info_list(count,j,rep) = info_list(count,j,rep)/sqrt(size(gResultSave,3));
                            case 'ListTemp_diff'
                                info_list(count,j,rep) = mean(gResultData.ListTemp_on-gResultData.ListTemp_off);
                            case 'ListTemp_diff_err'
                                info_list(count,j,rep) = std(gResultData.ListTemp_on-gResultData.ListTemp_off)/sqrt(length(gResultData.ListTemp_on));
                                info_list(count,j,rep) = info_list(count,j,rep)/sqrt(size(gResultSave,3));
                            case 'ListTemp_ave'
                                % directly calculate average temperature
                                % from counts, from both on and off
                                fl = mean(gResultData.FullListfl(1:4,:) + gResultData.FullListfl(8:-1:5,:),2);
                                info_list(count,j,rep) = (fl(1)+fl(2)-fl(3)-fl(4))./(fl(1)-fl(2)+fl(4)-fl(3))*delta_w/(-t_slope);
                            case 'ListTemp_ave_err'
                                info_list(count,j,rep) = std(gResultData.ListTemp_on+gResultData.ListTemp_off)/2;
                                info_list(count,j,rep) = info_list(count,j,rep)/sqrt(size(gResultSave,3));
                            case 'fl'   % fluorescence extracted from average of actual measurements
                                info_list(count,j,rep)=mean(gTrackCount(:,2));
                            case 'NVx'
                                info_list(count,j,rep)=NVloc(1);
                            case 'NVy'
                                info_list(count,j,rep)=NVloc(2);
                            case 'NVz'
                                info_list(count,j,rep)=NVloc(3);
                            case 'fcen'
                                info_list(count,j,rep)=(gSeqParameter.Freq1+...
                                    gSeqParameter.Freq2+gSeqParameter.delta_w)/2;
                            case 'linewidth'
                                info_list(count,j,rep)=gSeqParameter.Freq2-gSeqParameter.Freq1;
                            case 'ShotNoise_exp'
                                [~,info_list(count,j,rep),~]=get_shot_noise_limit(gResultData.FullListfl,...
                                    gSeqParameter,gResultData.MeasTime);
                            case 'ShotNoise_th'
                                [info_list(count,j,rep),~,~]=get_shot_noise_limit(gResultData.FullListfl,...
                                    gSeqParameter,gResultData.MeasTime);
                            otherwise
                                error('Data type not supported!')
                        end
                    end
                end
            end
        end
        info_list = mean(info_list,3);
end